﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Particle_Editor
{
    public partial class AddEditEmitterForm : Form
    {

        SGP.CSGP_Direct3D D3D = SGP.CSGP_Direct3D.GetInstance();
        SGP.CSGP_TextureManager TM = SGP.CSGP_TextureManager.GetInstance();
        
        Emitter temp = new Emitter("..\\..\\..\\SGP Project\\Resource\\Test.png");

        public int ImageID = -1;

        public String name { get; set; }
        public Rectangle rect { get; set; }
        public PointF point { get; set; }
        public String file { get; set; }
        public String SSystemPath { get; set; }

        public AddEditEmitterForm(String systemPath)
        {
            InitializeComponent();
            D3D.AddRenderTarget(splitContainer1.Panel1);
            D3D.AddRenderTarget(splitContainer1.Panel2);

            SSystemPath = systemPath;

            ImageID = TM.LoadTexture(temp.SFileName);
        }

        public AddEditEmitterForm(String emitterName, String fileName, int id, Rectangle rect, PointF point)
        {
            InitializeComponent();
            D3D.AddRenderTarget(splitContainer1.Panel1);
            D3D.AddRenderTarget(splitContainer1.Panel2);
            
            temp.SEmitterName = emitterName;
            emitterNameTextBox.Text = emitterName;
            temp.SFileName = fileName;
            filePathTextBox.Text = fileName;
            temp.ImageID = id;
            temp.flyweight.drawRect = rect;  
            temp.flyweight.centerPoint = point;
            ImageID = id;
        }

        private void splitContainer1_Panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                temp.flyweight.drawRect = new Rectangle(temp.flyweight.drawRect.Left, temp.flyweight.drawRect.Top, e.X - temp.flyweight.drawRect.Left, e.Y - temp.flyweight.drawRect.Top);
            }
            else if (e.Button == MouseButtons.Right)
            {
                temp.flyweight.centerPoint = new PointF(e.X, e.Y);
            }
            Render();
        }

        private void splitContainer1_Panel1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (e.X > temp.flyweight.drawRect.Left && e.Y > temp.flyweight.drawRect.Top) // Good rect
                    temp.flyweight.drawRect = new Rectangle(temp.flyweight.drawRect.Left, temp.flyweight.drawRect.Top, e.X - temp.flyweight.drawRect.Left, e.Y - temp.flyweight.drawRect.Top);
                else if (e.X > temp.flyweight.drawRect.Left) // Bad height
                    temp.flyweight.drawRect = new Rectangle(temp.flyweight.drawRect.Left, e.Y - temp.flyweight.drawRect.Height, temp.flyweight.drawRect.Width, temp.flyweight.drawRect.Height);
                else if (e.Y > temp.flyweight.drawRect.Top) // Bad width
                    temp.flyweight.drawRect = new Rectangle(e.X - temp.flyweight.drawRect.Width, temp.flyweight.drawRect.Top, temp.flyweight.drawRect.Width, temp.flyweight.drawRect.Height);
                else // Bad Both
                    temp.flyweight.drawRect = new Rectangle(e.X - temp.flyweight.drawRect.Width, e.Y - temp.flyweight.drawRect.Height, temp.flyweight.drawRect.Width, temp.flyweight.drawRect.Height);

                Render();
            }
        }

        private void splitContainer1_Panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                temp.flyweight.drawRect = new Rectangle(e.X, e.Y, 0, 0);
            }
            else if (e.Button == MouseButtons.Right)
            {
                temp.flyweight.centerPoint = new PointF(e.X, e.Y);
            }
            Render();
        }

        private void Render()
        {
            D3D.Clear(splitContainer1.Panel1, Color.Black);
            D3D.DeviceBegin();
            D3D.SpriteBegin();

            if (ImageID != -1)
            {
                TM.Draw(ImageID, 0, 0, 1, 1);
            }

            D3D.DrawHollowRect(temp.flyweight.drawRect, Color.Red, 1);
            D3D.DrawText("Left click and drag to create draw rect.", (splitContainer1.Panel1.Width >> 1) - 150, 5, Color.White);
            D3D.DrawText("Right click to set rotation point.", (splitContainer1.Panel1.Width >> 1) - 135, splitContainer1.Panel1.Height - 22, Color.White);

            Rectangle tempRect = new Rectangle((int)temp.flyweight.centerPoint.X - 2, (int)temp.flyweight.centerPoint.Y - 2, 5, 5);
            D3D.DrawRect(tempRect, Color.Magenta);
            D3D.SpriteEnd();
            D3D.DeviceEnd();
            D3D.Present();
        }

        private void AddEditEmitterForm_Enter(object sender, EventArgs e)
        {
            Render();
        }

        private void openButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.InitialDirectory = SSystemPath;
            open.Filter = "Images|*.bmp; *.jpg; *.png; *.tga";

            if (DialogResult.OK == open.ShowDialog())
            {
                filePathTextBox.Text = open.SafeFileName;
                temp.SFileName = open.SafeFileName;
                ImageID = TM.LoadTexture( SSystemPath + "\\" + temp.SFileName);
                Render();
                addButton.Enabled = true;
            }
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            if (filePathTextBox.Text.Length != 0)
            {
                name = emitterNameTextBox.Text;
                file = filePathTextBox.Text;
                rect = temp.flyweight.drawRect;
                point = temp.flyweight.centerPoint;
            }
        }
    }
}
